
export default {
  
  get(url,data){
    
    let token = uni.getStorageSync('token') //Storage获取
    console.log('get token ',token);
    return new Promise((resolve,reject) => {
      uni.request({
        method: 'GET',
        url:url,
        data: data,
        header: {
          "Authorization": token
        },
        timeout: 15000,
        dataType: 'json',
        success: (res) => {
          // console.log('get res ', res);
          if(res.data.code == 401){
            uni.hideLoading()
            // uni.removeStorageSync()//删除token
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                  // uni.navigateTo({
                  //   url:'/pages/example/login/index'
                  // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消1111');
              }
            })
            
          }
          if(res.data.code == 501){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消22222');
              }
            })
          }
          if(res.data.code == 502){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录失效，请重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消33333');
              }
            })
          }
          if(res.data.code == 500){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: "当前网络不稳定，请稍后重试",
              success: (res) => {
                if(res.confirm){
                  // uni.reLaunch({
                  //   url: '/pages/example/login/index'
                  // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消44444');
              }
            })
          }
          
          resolve(res.data)
        },
        fail: (err) => {
          uni.hideLoading()
          uni.showModal({
            title: '提示',
            content: '网络请求失败 请稍后重试 ',
            icon: 'none'
          })
          reject(err)
        }
      })
    })
  },
  
  post(url,data){
    let token = uni.getStorageSync('token'); //Storage获取
    // console.log('post方法',token);
    return new Promise((resolve,reject) => {
      uni.request({
        method: 'POST',
        url:url,
        data: data,
        timeout: 15000,
        header: {
          "Authorization": token
        },
        dataType: 'json',
        success: (res) => {
          if(res.data.code == 401){
            uni.hideLoading()
            // uni.removeStorageSync()//删除token
            uni.showModal({
              title:'提示',
              content: res.data.message,
              success: (res) => {
                if(res.confirm){
                  // uni.navigateTo({
                  //   url:'/pages/login/index'
                  // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消555555');
              }
            })
          }
          if(res.data.code == 501){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消66666');
              }
            })
          }
          if(res.data.code == 502){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录失效，请重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消7777777');
              }
            })
          }
          if(res.data.code == 500){
            console.log('进入到了这里 put 发送成功 返回值 500',res);
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: res.data.message,
              success: (res) => {
                if(res.confirm){
                    // uni.reLaunch({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消888888');
              }
            })
          }
          resolve(res.data)
        },
        fail: (err) => {
          uni.hideLoading()
          uni.showModal({
            title: '提示',
            content: '接口请求超时',
            icon: 'none',
            success(res) {
              if(res.confirm){
                uni.navigateTo({
                  url: '/pages/example/login/index.vue'
                })
              }
            }
          })
          reject(err)
        }
      })
    })
  },
  
  put(url,data){
    let token = uni.getStorageSync('token') //Storage获取
    // console.log('put 方法',token);
    return new Promise((resolve,reject) => {
      uni.request({
        method: 'PUT',
        url:url,
        data: data,
        timeout: 15000,
        header: {
          'content-type': 'application/json',
          "authorization": token
        },
        dataType: 'json',
        success: (res) => {
          if(res.data.code == 401){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                  // uni.navigateTo({
                  //   url:'/pages/example/login/index'
                  // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消9999999');
              }
            })
            
          }
          if(res.data.code == 501){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消1010');
              }
            })
          }
          if(res.data.code == 502){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录失效，请重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消11 11 11 11 ');
              }
            })
          }
          
          resolve(res.data)
        },
        fail: (err) => {
          uni.hideLoading()
          uni.showModal({
            title: '提示',
            content:'接口请求超时',
            icon: 'none'
          })
          reject(err)
        }
      })
    })
  },
  
  delete(url,data){
    let token = uni.getStorageSync('token') //Storage获取
    // console.log('put 方法',token);
    return new Promise((resolve,reject) => {
      uni.request({
        method: 'DELETE',
        url:url,
        data: data,
        timeout: 15000,
        header: {
          'content-type': 'application/json',
          "authorization": token
        },
        dataType: 'json',
        success: (res) => {
          if(res.data.code == 401){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                  // uni.navigateTo({
                  //   url:'/pages/example/login/index'
                  // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消9999999');
              }
            })
            
          }
          if(res.data.code == 501){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录过期重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消1010');
              }
            })
          }
          if(res.data.code == 502){
            uni.hideLoading()
            uni.showModal({
              title:'提示',
              content: '登录失效，请重新登录',
              success: (res) => {
                if(res.confirm){
                    // uni.navigateTo({
                    //   url: '/pages/example/login/index'
                    // })
                }
                else if(res.cancel)
                  console.log('用户点击了取消11 11 11 11 ');
              }
            })
          }
          
          resolve(res.data)
        },
        fail: (err) => {
          uni.hideLoading()
          uni.showModal({
            title: '提示',
            content:'接口请求超时',
            icon: 'none'
          })
          reject(err)
        }
      })
    })
  }
} 